﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace Utils
{
    public static partial class ExtensionHelper
    {
        /// <summary>
        /// 返回 整数(int32)
        /// </summary>
        /// <param name="str"></param>
        /// <param name="state">状态 true 表示成功 false 表示失败</param>
        /// <returns>int</returns>
        public static int ToInt32(this string str, out bool state)
        {
            var result = 0;
            state = int.TryParse(str, out result);
            return result;
        }
        /// <summary>
        /// 返回 整数(int32)
        /// </summary>
        /// <param name="str"></param>
        /// <returns>int</returns>
        public static int ToInt32(this string str)
        {
            bool state = false;
            return str.ToInt32(out state);
        }

        /// <summary>
        /// 返回整数（long）
        /// </summary>
        /// <param name="str"></param>
        /// <param name="state">状态 true 表示成功 false 表示失败</param>
        /// <returns>long</returns>
        public static long ToInt64(this string str, out bool state)
        {
            long result = 0;
            state = long.TryParse(str, out result);
            return result;
        }
        /// <summary>
        /// 返回整数（long）
        /// </summary>
        /// <param name="str"></param>
        /// <returns>long</returns>
        public static long ToInt64(this string str)
        {
            bool state = false;
            return str.ToInt64(out state);
        }

        /// <summary>
        /// 返回整数（ToDouble）
        /// </summary>
        /// <param name="str"></param>
        /// <param name="state">状态 true 表示成功 false 表示失败</param>
        /// <returns>double</returns>
        public static double ToDouble(this string str, out bool state)
        {
            double result = 0;
            state = double.TryParse(str, out result);
            return result;
        }
        /// <summary>
        /// 返回整数（ToDouble）
        /// </summary>
        /// <param name="str"></param>
        /// <returns>double</returns>
        public static double ToDouble(this string str)
        {
            bool state = false;
            return str.ToDouble(out state);
        }
        /// <summary>
        /// 返回整数（bool）
        /// </summary>
        /// <param name="str"></param>
        /// <param name="state">状态 true 表示成功 false 表示失败</param>
        /// <returns>bool</returns>
        public static bool ToBool(this string str, out bool state)
        {
            state = bool.TryParse(str, out var result);
            return result;
        }
        /// <summary>
        /// 返回整数（bool）
        /// </summary>
        /// <param name="str"></param>
        /// <returns>bool</returns>
        public static bool ToBool(this string str)
        {
            bool state = false;
            return str.ToBool(out state);
        }

        /// <summary>
        /// 返回格式为(yyyy-MM-dd HH:mm:ss)的时间字符串
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        public static string ToString2(this DateTime dt) => dt.ToString("yyyy-MM-dd HH:mm:ss");


        /// <summary>
        /// 返回格式为(yyyy-MM-dd HH:mm:ss)的时间字符串
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        public static string ToStringLongDate(this DateTime dt) => dt.ToString("yyyy-MM-dd HH:mm:ss");

        /// <summary>
        /// 返回格式为(yyyy-MM-dd)的时间字符串
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        public static string ToStringDate(this DateTime dt) => dt.ToString("yyyy-MM-dd");

        /// <summary>
        /// 返回时间（DateTime）
        /// </summary>
        /// <param name="str"></param>
        /// <param name="state">状态 true 表示成功 false 表示失败</param>
        /// <returns>DateTime</returns>
        public static DateTime ToDateTime(this string str, out bool state)
        {
            DateTime result = DateTime.Now;
            state = DateTime.TryParse(str, out result);
            return result;
        }

        /// <summary>
        /// 获取枚举类型的字符串
        /// </summary>
        /// <param name="obj">枚举对象</param>
        /// <returns>string</returns>
        public static string ToString(this System.Enum obj) => System.Enum.GetName(obj.GetType(), obj);

        /// <summary>
        /// 扩展EndTrim
        /// </summary>
        /// <param name="str">字符串</param>
        /// <param name="trimstr">需要去除的字符串</param>
        /// <returns></returns>
        public static string Trim(this string str, string trimstr)
        {
            return str.Trim(trimstr.Trim().ToCharArray()); ;
        }
        #region TrimEnd
        /// <summary>
        /// 扩展EndTrim
        /// </summary>
        /// <param name="str">字符串</param>
        /// <param name="trimstr">末尾需要去除的字符串</param>
        /// <returns></returns>
        public static string TrimEnd(this string str, string trimstr)
        {
            return str.TrimEnd(trimstr.Trim().ToCharArray()); ;
        }
        #endregion

        #region TrimStart
        /// <summary>
        /// 扩展EndTrim
        /// </summary>
        /// <param name="str">字符串</param>
        /// <param name="trimstr">字符串开始需要去除的字符串</param>
        /// <returns></returns>
        public static string TrimStart(this string str, string trimstr)
        {
            return str.TrimStart(trimstr.Trim().ToCharArray());
        }
        #endregion


        #region ToString  把 List<T> 按照分隔符组装成 string
        /// <summary>
        /// 把 List<T> 按照分隔符组装成 string
        /// </summary>
        /// <param name="list"></param>
        /// <param name="speater"></param>
        /// <returns></returns>
        public static string ToString<T>(this List<T> list, string speater)
        {
            var sb = new StringBuilder();
            for (var i = 0; i < list.Count; i++)
            {
                sb.Append(i == list.Count - 1 ? list[i].ToString() : $"{list[i]}{speater}");
            }
            return sb.ToString();
        }
        #endregion




        /// <summary>
        /// 检查字符串最大长度，返回指定长度的串
        /// </summary>
        /// <param name="input">输入字符串</param>
        /// <param name="maxLength">最大长度</param>
        /// <returns>返回值</returns>			
        public static string Split(this string input, int maxLength)
        {
            if (!string.IsNullOrEmpty(input))
            {
                input = input.Trim();
                if (input.Length > maxLength) //按最大长度截取字符串
                {
                    input = input.Substring(0, maxLength);
                }
            }
            return input;
        }

        #region  ToSBC 转全角的函数(SBC case)  
        /// <summary>  
        /// 转全角的函数(SBC case)  
        /// </summary>  
        /// <param name="str"></param>  
        /// <returns></returns>  
        public static string ToSBC(this string str)
        {
            string a = "";
            //半角转全角：  
            char[] c = str.ToCharArray();
            for (int i = 0; i < c.Length; i++)
            {
                if (c[i] == 32)
                {
                    c[i] = (char)12288;
                    continue;
                }
                if (c[i] < 127)
                    c[i] = (char)(c[i] + 65248);
            }
            return new string(c);
        }
        #endregion

        #region ToDBC 转半角的函数(SBC case)  
        /// <summary>  
        ///  转半角的函数(SBC case)  
        /// </summary>  
        /// <param name="str">输入</param>  
        /// <returns></returns>  
        public static string ToDBC(this string str)
        {
            char[] c = str.ToCharArray();
            for (int i = 0; i < c.Length; i++)
            {
                if (c[i] == 12288)
                {
                    c[i] = (char)32;
                    continue;
                }
                if (c[i] > 65280 && c[i] < 65375)
                    c[i] = (char)(c[i] - 65248);
            }
            return new string(c);
        }
        #endregion





        public static string ToStringEx(this Exception exception)
        {

            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.AppendLine("====================EXCEPTION====================");
            stringBuilder.AppendLine("【Message】:" + exception.Message);
            stringBuilder.AppendLine("【Source】:" + exception.Source);
            stringBuilder.AppendLine("【TargetSite】:" + ((exception.TargetSite != null) ? exception.TargetSite.Name : "无"));
            stringBuilder.AppendLine("【StackTrace】:" + exception.StackTrace);
            stringBuilder.AppendLine("【exception】:" + exception);
            stringBuilder.AppendLine("=================================================");
            if (exception.InnerException != null)
            {
                stringBuilder.AppendLine("====================INNER EXCEPTION====================");
                stringBuilder.AppendLine("【Message】:" + exception.InnerException.Message);
                stringBuilder.AppendLine("【Source】:" + exception.InnerException.Source);
                stringBuilder.AppendLine("【TargetSite】:" + ((exception.InnerException.TargetSite != null) ? exception.InnerException.TargetSite.Name : "无"));
                stringBuilder.AppendLine("【StackTrace】:" + exception.InnerException.StackTrace);
                stringBuilder.AppendLine(("【InnerException】:" + exception.InnerException) ?? "");
                stringBuilder.AppendLine("=================================================");
            }
            return stringBuilder.ToString().Replace("/r", "").Replace("/n", "");
        }


    }
}
